﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data.Linq;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1.Alarm
{
    public partial class FormAlarmDetect : Form
    {
        private tb_alarm _item;
        
        public FormAlarmDetect(tb_alarm item)
        {
            InitializeComponent();
            this.skinEngine1.SkinFile = "MP10.ssk";

            _item = item;
        }

        private string getMonitorDeviceId(string MonitorDev_Id)
        {
            try
            {
                var queryNum =
                from num in Monitor_DB.instance.db.tb_monitor_device
                where num.ID == Convert.ToInt32(MonitorDev_Id)
                select num;

                return queryNum.First().Name;
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message, "错误！", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return null;
            }
        }

        private void FormAlarmDetect_Load(object sender, EventArgs e)
        {
            try
            {
                // Alarm ID     
                this.iDTextBox.Text = _item.ID.ToString();
                this.device_IdTextBox.Text = _item.Device_Id.ToString();
                this.monitorDevice_IdTextBox.Text = getMonitorDeviceId(_item.MonitorDevice_Id.ToString());

                // Alarm device

                this.nameTextBox.Text = _item.Name;
                this.sensorTextBox.Text = _item.Sensor;

                // Alarm value
                this.valueTextBox.Text = _item.Value.ToString();
                this.thresholdTextBox.Text = _item.Threshold.ToString();
                this.threshold_TwoTextBox.Text = _item.Threshold_Two.ToString();
                this.threshold_ThreeTextBox.Text = _item.Threshold_Three.ToString();

                if (_item.Alarm_Type != null)
                {
                    this.alarm_TypeTextBox.Text = _item.Alarm_Type.ToString();
                }

                if (_item.Alarm_Time != null)
                {
                    this.alarm_TimeDateTimePicker.Text = _item.Alarm_Time.ToString();
                }

                // Alarm pic
                switch (_item.Alarm_Type)
                {
                    case "高":
                        this.pictureBox_alarmType.Image = WindowsFormsApplication1.Properties.Resources.RED;
                        
                        break;
                    case "中":
                        this.pictureBox_alarmType.Image = WindowsFormsApplication1.Properties.Resources.YELLOW;
   
                        break;
                    case "低":
                        this.pictureBox_alarmType.Image = WindowsFormsApplication1.Properties.Resources.GREEN;
          
                        break;

                    default:
                        break;
                }

                if (_item.Checked == false)
                {
                    Alarm.alarmDetect.instance.playAlarmSound();
                }

            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message, "错误！", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                string alarmNote = "传感器:" + _item.Sensor + "    " + "告警类型:" + _item.Alarm_Type + "    " + "告警序号:" + _item.ID.ToString();

                if (_item.Checked == false)
                {
                    _item.Checked = true;
                    Monitor_DB.instance.db.SubmitChanges(ConflictMode.FailOnFirstConflict);

                    UserLog.adduseroperlog(User.UserID, User.Username, "告警确认", "成功", getMonitorDeviceId(_item.MonitorDevice_Id.ToString()),
                                       _item.Device_Id.ToString(), alarmNote);
                }
                
                Alarm.alarmDetect.instance.stopAlarmSound();

            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message, "错误！", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            
        
            this.Dispose();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                Alarm.alarmDetect.instance.stopAlarmSound();
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message, "错误！", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            this.Dispose();
        }

        private void FormAlarmDetect_FormClosed(object sender, FormClosedEventArgs e)
        {
            try
            {
                Alarm.alarmDetect.instance.stopAlarmSound();
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message, "错误！", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void FormAlarmDetect_FormClosing(object sender, FormClosingEventArgs e)
        {

        }
    }
}
